<?php


namespace app\agent\controller\task;

use app\common\model\plan\Plan;
use EasyWeChat\Factory;
use app\common\library\Auth;
use think\Env;
use app\common\controller\Manage;

class Outer extends Manage
{
    protected $noNeedLogin = ['config','oauth','login','testlogin','index','add'];
    protected $noNeedRight = ['*'];
    protected $app = '';
    protected $appconfig = '';
    protected $ModelSchool;
    protected $ModelPersonnel;
    protected $contollerName = 'task/outer';//控制器回调
    protected $where;

    public function _initialize()
    {
        $this->appconfig = $this->config();
        $this->app = Factory::work($this->config());
        $this->auth = Auth::instance();
        $this->model = new Plan();
        $this->where['status'] = 2;
        parent::_initialize();
    }

    public function testlogin()
    {
        //访问index 请求授权登录
        $this->auth->IndexLogin('yangliu');
        return $this->redirect('index');
    }

    public function index()
    {
        $list = $this->model->where($this->where)->select();
        //查询周计划
//        echo "<pre>";
//        print_r(collection($list)->toArray());exit;
        return $this->view->fetch();
    }


    public function add()
    {
        $this->view->assign('jssdk',$this->jssdk());
        return $this->view->fetch();
    }

    private function jssdk()
    {
        $url = 'http://oa.fc62.com'.$this->request->url();
        $app = Factory::work($this->config());
        $app->jssdk->setUrl($url);
        $json = $app->jssdk->buildConfig(array('checkJsApi','selectEnterpriseContact','openEnterpriseChat','getLocation','selectEnterpriseContact'),true,true);
//        $data = json_decode($json,true);
        return $json;
    }

    private  function config()
    {
        $config = [
            'agent_id' => Env::get('school.agent_id'),
            'corp_id' => Env::get('user.corp_id'),
            'secret'   => Env::get('school.secret'),
            'response_type' => Env::get('school.response_type'),
            'log' => [
                'level' => Env::get('school.level'),
                'file' => __DIR__.Env::get('school.file'),
            ],
            'oauth'=> [
                'scopes'=> Env::get('school.scopes'),
                'callback' => Env::get('school.callback')
            ]
        ];
        return $config;
    }

    public function oauth()
    {
        $token = $this->app->access_token->getToken();//获取企业微信token
        $code = $this->request->request('code');
        $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={$token['access_token']}&code={$code}";
        $user = \fast\Http::get($url);
        if(!empty($user)) $user = json_decode($user,true);
        if($user['errcode'] == 0){
            if(isset($user['UserId'])){
                if(!$this->auth->IndexLogin($user['UserId'])){
                    $this->error(__('您的账户不存在，请联系管理员'));
                }else{
                    return redirect('/agent/school/index');
                }
            }
        }else{
            return redirect($this->request->domain().'/agent/school/index');
        }
    }

    public function login()
    {
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appconfig['corp_id']."&redirect_uri=".$this->appconfig['oauth']['callback']."&response_type=code&scope=".$this->appconfig['oauth']['scopes']."&agentid=".$this->appconfig['agent_id']."&state=1#wechat_redirect";
        return redirect($url);
    }

}